package com.kbplus.demo.data.permission.service;

import cn.hutool.json.JSONObject;
import cn.hutool.jwt.JWT;
import cn.hutool.jwt.JWTUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

@Slf4j
@Component
public class ConcurrentUserService {

    public static JSONObject concurrentUser() {
        JSONObject user = new JSONObject();
        try {
            ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
            HttpServletRequest request = requestAttributes.getRequest();
            String token = request.getHeader("Authorization");
            if (StringUtils.isBlank(token)) {
                token = request.getParameter("Authorization");
            }
            if (StringUtils.isNoneBlank(token)) {
                JWT jwt = JWTUtil.parseToken(token);
                return jwt.getPayloads();
            } else {
                log.error("获取token为空");
            }
        } catch (IllegalStateException e) {
            e.printStackTrace();
            log.error("没有请求");
        }
        return user;
    }
}
